package com.czk.shop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.comon.czk.constant.ShopCode;
import com.comon.czk.entity.Result;
import com.comon.czk.exception.CastException;
import com.czk.api.IUserService;
import com.czk.shop.mapper.TradeUserMapper;
import com.czk.shop.mapper.TradeUserMoneyLogMapper;
import com.czk.shop.pojo.TradeUser;
import com.czk.shop.pojo.TradeUserMoneyLog;
import com.czk.shop.pojo.TradeUserMoneyLogExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 用户接口的实现类
 * @Author:ChenZhangKun
 * @Date: 2020/9/18 21:26
 */
@Component
@Service(interfaceClass = IUserService.class)
public class IUserServiceImpl implements IUserService {
    @Autowired
    private TradeUserMapper tradeUserMapper;
    @Autowired
    private TradeUserMoneyLogMapper tradeUserMoneyLogMapper;
    @Override
    public TradeUser findOne(Long userId) {
        if (userId==null){
            CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID);
        }
        TradeUser tradeUser = tradeUserMapper.selectByPrimaryKey(userId);
        return tradeUser;
    }

    @Override
    public Result updateMoneyPaid(TradeUserMoneyLog tradeUserMoneyLog) {
        // 校验参数是否合法
        // 微服务第一步都是校验参数是否合法
        if (tradeUserMoneyLog==null||
                tradeUserMoneyLog.getUserId()==null||
                tradeUserMoneyLog.getOrderId()==null||
                tradeUserMoneyLog.getUseMoney()==null||
                tradeUserMoneyLog.getUseMoney().compareTo(BigDecimal.ZERO)<=0){
            CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID);
        }
        // 查询订单余额使用日志
        TradeUserMoneyLogExample tradeUserMoneyLogExample=new TradeUserMoneyLogExample();
        TradeUserMoneyLogExample.Criteria criteria = tradeUserMoneyLogExample.createCriteria();
        criteria.andOrderIdEqualTo(tradeUserMoneyLog.getOrderId());
        criteria.andUserIdEqualTo(tradeUserMoneyLog.getUserId());
        int i = tradeUserMoneyLogMapper.countByExample(tradeUserMoneyLogExample);
        // 查看用户余额
        TradeUser tradeUser = tradeUserMapper.selectByPrimaryKey(tradeUserMoneyLog.getUserId());
        // 扣减余额
        if (tradeUserMoneyLog.getMoneyLogType().intValue()==ShopCode.SHOP_USER_MONEY_PAID.getCode().intValue()){
            // i>0 表示已经付款，此时已经更新了付款日志，不用再付款
            if (i>0){
                // 已经付款
                CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_IS_PAY);
            }
            // 扣减余额
            tradeUser.setUserMoney(new BigDecimal(tradeUser.getUserMoney()).subtract(tradeUserMoneyLog.getUseMoney()).longValue());
            // 更新用户
            tradeUserMapper.updateByPrimaryKey(tradeUser);
        }
        // 回退余额
        if (tradeUserMoneyLog.getMoneyLogType().intValue()==ShopCode.SHOP_USER_MONEY_REFUND.getCode().intValue()){
            // i<0此时没有付款，无法退款
            if (i==0){
                CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_NO_PAY);
            }
            // 防止多次退款
            TradeUserMoneyLogExample tradeUserMoneyLogExample1=new TradeUserMoneyLogExample();
            TradeUserMoneyLogExample.Criteria criteria1 = tradeUserMoneyLogExample1.createCriteria();
            criteria1.andUserIdEqualTo(tradeUserMoneyLog.getUserId());
            criteria1.andOrderIdEqualTo(tradeUserMoneyLog.getOrderId());
            criteria1.andMoneyLogTypeEqualTo(ShopCode.SHOP_USER_MONEY_REFUND.getCode());
            int j = tradeUserMoneyLogMapper.countByExample(tradeUserMoneyLogExample1);
            // j>表示已经退过款
            if (j>0){
                CastException.cast(ShopCode.SHOP_USER_MONEY_REFUND_ALREADY);
            }
            // 执行退款操作加余额
            tradeUser.setUserMoney(new BigDecimal(tradeUser.getUserMoney()).add(tradeUserMoneyLog.getUseMoney()).longValue());
            // 更新用户
            tradeUserMapper.updateByPrimaryKey(tradeUser);
        }
        // 记录订单余额使用日志
        tradeUserMoneyLog.setCreateTime(new Date());
        tradeUserMoneyLogMapper.insert(tradeUserMoneyLog);
        return new Result(ShopCode.SHOP_SUCCESS.getSuccess(),ShopCode.SHOP_SUCCESS.getMessage());
    }
}
